import 'dart:io';
import 'package:fate_blind_box/chat_view/ahu_text.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../theme/theme_color.dart';

class PrivacyWebView extends StatefulWidget {
  const PrivacyWebView(this.title, this.url, {Key? key}) : super(key: key);

  final String title;
  final String url;

  @override
  _BrowserState createState() => _BrowserState();
}

class _BrowserState extends State<PrivacyWebView> {
  final GlobalKey webViewKey = GlobalKey();
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
      useShouldOverrideUrlLoading: true,
      mediaPlaybackRequiresUserGesture: false,
    ),

    /// android 支持HybridComposition
    android: AndroidInAppWebViewOptions(
      useHybridComposition: true,
    ),
    ios: IOSInAppWebViewOptions(
      allowsInlineMediaPlayback: true,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
      title: widget.title,
      backAction: () {
        Get.back();
      },
      childWidget: Column(
        children: [
          Expanded(
              child: InAppWebView(
            key: webViewKey,
            initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
            initialOptions: options,
          )),
          Container(
            color: Colors.white,
            padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                getBottomBtn(
                    bgColor: ThemeColor.appColor,
                    title: '已知晓',
                    titleColor: Colors.white,
                    tapAction: () {
                      // backBtnAction(context, '');
                      showDialog(
                          context: context,
                          barrierDismissible: false,
                          builder: (context) {
                            return const TipsView();
                          }).then((value){
                            if(value =='同意'){
                              Navigator.of(context).pop();
                            }
                      });
                    }),
                getBottomBtn(
                    bgColor: ThemeColor.checkNormalColor,
                    title: '不同意',
                    titleColor: Colors.black,
                    tapAction: () {
                      exit(0);
                    }),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget getBottomBtn(
      {Color? bgColor,
      String title = '',
      Color? titleColor,
      Function? tapAction}) {
    return Container(
        width: 100,
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.circular(10),
        ),
        child: TextButton(
            onPressed: () {
              if (tapAction != null) {
                tapAction();
              }
            },
            child: Text(
              title,
              style: TextStyle(color: titleColor),
            )));
  }

  Future<void> backBtnAction(context, String title) async {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: const Text.rich(TextSpan(children: [
              TextSpan(
                  text:
                      "请仔细阅读并同意《隐私协议》,一些第三方的运营商存在收集IMEI、设备MAC地址和软件安装列表、通讯录和短信的行为，如果不同意，您将无法正常使用APP",
                  style: TextStyle(color: Color(0xFF222222), fontSize: 12)),
            ])),
            actions: <Widget>[
              CupertinoDialogAction(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text(
                  '同意并继续',
                  style: TextStyle(fontSize: 16, color: ThemeColor.appColor),
                ),
              ),
              CupertinoDialogAction(
                onPressed: () {
                  exit(0);
                },
                child: const Text(
                  '不同意',
                  style: TextStyle(fontSize: 16, color: Colors.black),
                ),
              ),
            ],
          );
        });
  }
}

class TipsView extends StatelessWidget {
  const TipsView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.center,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 40.w),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(12.w))),
        padding: EdgeInsets.symmetric(horizontal: 25.0.w, vertical: 12.w),
        child: SafeArea(
          bottom: true,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(height: 6.w),
              AhuText(
                text:
                    '请仔细阅读并同意《隐私协议》,一些第三方的运营商存在收集IMEI、设备MAC地址和软件安装列表、通讯录和短信的行为，如果不同意，您将无法正常使用APP"',
                color: '333333'.stringToColor,
                fontSize: 13,
                fontWeight: FontWeight.w600,
                maxLines: 13,
              ),
              SizedBox(height: 18.w),
              Divider(
                color: '999999'.stringToColor,
                height: 1.w,
              ),
              SizedBox(height: 12.w),
              Row(
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: (){
                        Navigator.pop(context,'同意');
                      },
                      child: AhuText(
                        text: '同意并继续',
                        color: ThemeColor.appColor,
                        fontSize: 16,
                        fontWeight: FontWeight.w400,
                        maxLines: 3,
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                  Container(
                    width: 1.w,
                    height: 24.w,
                    color: '999999'.stringToColor,
                  ),
                  Expanded(
                    child: InkWell(
                      onTap: (){
                        Navigator.of(context).pop('返回');
                      },
                      child: AhuText(
                        text: '返回',
                        color: '333333'.stringToColor,
                        fontSize: 16,
                        fontWeight: FontWeight.w400,
                        maxLines: 3,
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
